/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import dao.StudentDao;
import dao.StudentDaoJTableImpl;
import entity.Student;

/**
 * 
 * @author 柯智青
 */
public class Search extends javax.swing.JFrame {

	/**
	 * Creates new form Search
	 */
	public Search() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jComboBox1 = new javax.swing.JComboBox<>();
		jLabel1 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		findButton = new javax.swing.JButton();
		jScrollPane2 = new javax.swing.JScrollPane();
		jTable2 = new javax.swing.JTable();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("查询界面");

		jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(
				new String[] { "学号", "姓名", "宿舍号" }));
		jComboBox1.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				jComboBox1ItemStateChanged(evt);
			}
		});
		jComboBox1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jComboBox1ActionPerformed(evt);
			}
		});

		jLabel1.setText("查询方式");

		jTextField1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jTextField1ActionPerformed(evt);
			}
		});

		findButton.setText("搜索");

		findButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				findButtonActionPerformed(evt);
			}
		});

		jTable2.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "学号", "姓名", "性别", "出生日期", "政治面貌", "家庭住址",
						"电话", "宿舍号" }));
		jScrollPane2.setViewportView(jTable2);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(95, 95, 95)
								.addComponent(jLabel1,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										63, Short.MAX_VALUE)
								.addGap(18, 18, 18)
								.addComponent(jComboBox1, 0, 77,
										Short.MAX_VALUE)
								.addGap(27, 27, 27)
								.addComponent(jTextField1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										80,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(33, 33, 33)
								.addComponent(findButton,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										72, Short.MAX_VALUE)
								.addGap(147, 147, 147))
				.addGroup(
						layout.createSequentialGroup().addContainerGap()
								.addComponent(jScrollPane2).addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(44, 44, 44)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel1)
												.addComponent(
														jComboBox1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														jTextField1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(findButton))
								.addGap(18, 18, 18)
								.addComponent(jScrollPane2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		pack();
	}

	private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {

		if (jComboBox1.getItemAt(index).equals("学号")) {
			searchKey = "num";
		} else if (jComboBox1.getItemAt(index).equals("姓名")) {
			searchKey = "name";
		} else {
			searchKey = "dormNum";
		}

	}

	private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {

		index = jComboBox1.getSelectedIndex();
	}

	// 根据查询关键字获取数据库中相匹配的信息，并将信息显示在表格上
	private void findButtonActionPerformed(java.awt.event.ActionEvent evt) {
		String searchValue = jTextField1.getText();
		DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
		StudentDao stuDao = new StudentDaoJTableImpl(jTable2);
		ArrayList<Student> stulist = stuDao.searchStudents(searchKey,
				searchValue);
		if (stulist.size() == 0) {
			JOptionPane.showMessageDialog(null, "您要查找的信息不存在");
		} else {
			for (Student student : stulist) {
				String[] strs = stuDao.convertStudentToStringArr(student);
				model.addRow(strs);
			}
		}

	}

	private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main() {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(Search.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(Search.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(Search.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(Search.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Search().setVisible(true);
			}
		});
	}

	private String searchKey;
	private int index = 0;
	// Variables declaration - do not modify
	private javax.swing.JComboBox<String> jComboBox1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTable jTable2;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JButton findButton;
	// End of variables declaration
}
